package cn.iocoder.yudao.module.trade.controller.admin.ordersafetycheckterm;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.ordersafetycheckterm.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.ordersafetycheckterm.OrderSafetyCheckTermDO;
import cn.iocoder.yudao.module.trade.service.ordersafetycheckterm.OrderSafetyCheckTermService;

@Tag(name = "管理后台 - 安检项详情")
@RestController
@RequestMapping("/trade/order-safety-check-term")
@Validated
public class OrderSafetyCheckTermController {

    @Resource
    private OrderSafetyCheckTermService orderSafetyCheckTermService;

    @PostMapping("/create")
    @Operation(summary = "创建安检项")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-term:create')")
    public CommonResult<Long> createOrderSafetyCheckTerm(@Valid @RequestBody OrderSafetyCheckTermSaveReqVO createReqVO) {
        return success(orderSafetyCheckTermService.createOrderSafetyCheckTerm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新安检项")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-term:update')")
    public CommonResult<Boolean> updateOrderSafetyCheckTerm(@Valid @RequestBody OrderSafetyCheckTermRespVO updateReqVO) {
        orderSafetyCheckTermService.updateOrderSafetyCheckTerm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除安检项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-term:delete')")
    public CommonResult<Boolean> deleteOrderSafetyCheckTerm(@RequestParam("id") Long id) {
        orderSafetyCheckTermService.deleteOrderSafetyCheckTerm(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得安检项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-term:query')")
    public CommonResult<OrderSafetyCheckTermRespVO> getOrderSafetyCheckTerm(@RequestParam("id") Long id) {
        OrderSafetyCheckTermDO orderSafetyCheckTerm = orderSafetyCheckTermService.getOrderSafetyCheckTerm(id);
        return success(BeanUtils.toBean(orderSafetyCheckTerm, OrderSafetyCheckTermRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得安检项分页")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-term:query')")
    public CommonResult<PageResult<OrderSafetyCheckTermRespVO>> getOrderSafetyCheckTermPage(@Valid OrderSafetyCheckTermPageReqVO pageReqVO) {
        PageResult<OrderSafetyCheckTermDO> pageResult = orderSafetyCheckTermService.getOrderSafetyCheckTermPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderSafetyCheckTermRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出安检项 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-term:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderSafetyCheckTermExcel(@Valid OrderSafetyCheckTermPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderSafetyCheckTermDO> list = orderSafetyCheckTermService.getOrderSafetyCheckTermPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "安检项.xls", "数据", OrderSafetyCheckTermRespVO.class,
                        BeanUtils.toBean(list, OrderSafetyCheckTermRespVO.class));
    }

}